import { PolylineEdge, PolylineEdgeModel,h } from '@logicflow/core'
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var directionMap = {
    tr: 'tl',
    lb: 'tl',
    tl: 'tr',
    rb: 'tr',
    br: 'bl',
    lt: 'bl',
    bl: 'br',
    rt: 'br',
};
function pointFilter(points) {
    var all = points;
    var i = 1;
    while (i < all.length - 1) {
        var _a = __read(all[i - 1], 2), x = _a[0], y = _a[1];
        var _b = __read(all[i], 2), x1 = _b[0], y1 = _b[1];
        var _c = __read(all[i + 1], 2), x2 = _c[0], y2 = _c[1];
        if ((x === x1 && x1 === x2) || (y === y1 && y1 === y2)) {
            all.splice(i, 1);
        }
        else {
            i++;
        }
    }
    return all;
}
function getMidPoints(cur, key, orientation, radius) {
    var mid1 = [cur[0], cur[1]];
    var mid2 = [cur[0], cur[1]];
    switch (orientation) {
        case 'tl': {
            if (key === 'tr') {
                mid1[1] += radius;
                mid2[0] += radius;
            }
            else if (key === 'lb') {
                mid1[0] += radius;
                mid2[1] += radius;
            }
            return [mid1, mid2];
        }
        case 'tr': {
            if (key === 'tl') {
                mid1[1] += radius;
                mid2[0] -= radius;
            }
            else if (key === 'rb') {
                mid1[0] -= radius;
                mid2[1] += radius;
            }
            return [mid1, mid2];
        }
        case 'bl': {
            if (key === 'br') {
                mid1[1] -= radius;
                mid2[0] += radius;
            }
            else if (key === 'lt') {
                mid1[0] += radius;
                mid2[1] -= radius;
            }
            return [mid1, mid2];
        }
        case 'br': {
            if (key === 'bl') {
                mid1[1] -= radius;
                mid2[0] -= radius;
            }
            else if (key === 'rt') {
                mid1[0] -= radius;
                mid2[1] -= radius;
            }
            return [mid1, mid2];
        }
        default:
            return [];
    }
}
function getPartialPath(prev, cur, next, radius) {
    var _a;
    var dir1 = '';
    var dir2 = '';
    if (prev[0] === cur[0]) {
        dir1 = prev[1] > cur[1] ? 't' : 'b';
    }
    else if (prev[1] === cur[1]) {
        dir1 = prev[0] > cur[0] ? 'l' : 'r';
    }
    if (cur[0] === next[0]) {
        dir2 = cur[1] > next[1] ? 't' : 'b';
    }
    else if (cur[1] === next[1]) {
        dir2 = cur[0] > next[0] ? 'l' : 'r';
    }
    var r = Math.min(Math.hypot(cur[0] - prev[0], cur[1] - prev[1]) / 2, Math.hypot(next[0] - cur[0], next[1] - cur[1]) / 2, radius) || (1 / 5) * radius;
    var key = "".concat(dir1).concat(dir2);
    var orientation = directionMap[key] || '-';
    var path = "L ".concat(prev[0], " ").concat(prev[1]);
    if (orientation === '-') {
        path += "L ".concat(cur[0], " ").concat(cur[1], " L ").concat(next[0], " ").concat(next[1]);
    }
    else {
        var _b = __read(getMidPoints(cur, key, orientation, r), 2), mid1 = _b[0], mid2 = _b[1];
        if (mid1 && mid2) {
            path += "L ".concat(mid1[0], " ").concat(mid1[1], " Q ").concat(cur[0], " ").concat(cur[1], " ").concat(mid2[0], " ").concat(mid2[1]);
            _a = __read(mid2, 2), cur[0] = _a[0], cur[1] = _a[1];
        }
    }
    return path;
}
function getCurvedEdgePath(points, radius) {
    var i = 0;
    var d = '';
    if (points.length === 2) {
        d += "M".concat(points[i][0], " ").concat(points[i++][1], " L ").concat(points[i][0], " ").concat(points[i][1]);
    }
    else {
        d += "M".concat(points[i][0], " ").concat(points[i++][1]);
        for (; i + 1 < points.length;) {
            var prev = points[i - 1];
            var cur = points[i];
            var next = points[i++ + 1];
            d += getPartialPath(prev, cur, next, radius);
        }
        d += "L ".concat(points[i][0], " ").concat(points[i][1]);
    }
    return d;
}
class CurvedEdge extends PolylineEdge {
    constructor() {
        super(...arguments);         
    }
    getEdge() {
        const model = this.props.model;
        const pointsStr = model.points;
        const isAnimation = model.isAnimation;
        const arrowConfig = model.arrowConfig;
        const radius = model.radius || 5;
        const style = model.getEdgeStyle();
        const animationStyle = model.getEdgeAnimationStyle();
        const points = pointFilter(pointsStr.split(' ').map(p => p.split(',').map(a => +a)));
        const d = getCurvedEdgePath(points, radius);
        // console.log('points',pointsStr,radius)
        const attrs = {
            ...style,
            ...arrowConfig,
            fill: 'none',
            ...(isAnimation ? animationStyle : {}),
            d: d
        };
        return h('path', attrs);
    }
}

class CurvedEdgeModel extends PolylineEdgeModel {
    constructor() {
        super(...arguments);
    }
}

export {CurvedEdge, CurvedEdgeModel };
